//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.Caching;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Configuration;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Reader.Components;
using FormLabel=CommunityServer.ControlPanel.Controls.FormLabel;
using HelpIcon=CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceManager=CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Reader
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings : BaseReaderAdminPage
	{
		protected TextBox lastModifiedInterval;
		protected TextBox truncationLength;
		protected LinkButton SaveButton;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected MPContent DescriptionRegion;
		protected HelpIcon Helpicon1;
		protected FormLabel Formlabel2;
		protected RequiredFieldValidator Requiredfieldvalidator1;
		protected HelpIcon Helpicon3;
		protected FormLabel Formlabel3;
		protected HelpIcon Helpicon4;
		protected FormLabel Formlabel4;
		protected HelpIcon Helpicon5;
		protected FormLabel Formlabel5;
		protected HelpIcon Helpicon6;
		protected FormLabel Formlabel6;
		protected HelpIcon Helpicon2;
		protected FormLabel Formlabel1;
		protected RequiredFieldValidator Requiredfieldvalidator2;
		protected HelpIcon Helpicon7;
		protected FormLabel Formlabel7;
		protected TextBox pagerSize;
		protected RequiredFieldValidator Requiredfieldvalidator3;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected HelpIcon Helpicon8;
		protected FormLabel Formlabel8;
		protected TextBox rssResultSize;
		protected RequiredFieldValidator Requiredfieldvalidator4;
		protected FileOnlyStatusMessage FOStatus;
	
		private void Page_Load(object sender, EventArgs e)
		{
			SaveButton.Text = ResourceManager.GetString("Save");
			if (!Page.IsPostBack)
			{
				LoadConfiguration();

				if (ReaderConfiguration.Instance().FileOnly)
				{
					FOStatus.Visible = true;
				}
			}
		}

		private void LoadConfiguration()
		{
			ReaderConfiguration config = ReaderConfiguration.Instance(!ReaderConfiguration.Instance().FileOnly);

			lastModifiedInterval.Text	= config.LastModifiedInterval.ToString();
			truncationLength.Text		= config.TruncationLength.ToString();
			pagerSize.Text				= config.PageSize.ToString();
			rssResultSize.Text			= config.RSSSize.ToString();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if (Page.IsValid)
			{

				ReaderConfiguration config = ReaderConfiguration.Instance();


				if (!config.FileOnly)
				{
					config.LastModifiedInterval = Int32.Parse(lastModifiedInterval.Text);
					config.TruncationLength		= Int32.Parse(truncationLength.Text);
					config.PageSize				= Int32.Parse(pagerSize.Text);
					config.RSSSize				= Int32.Parse(rssResultSize.Text);

					CommonDataProvider dp = CommonDataProvider.Instance();
					dp.CreateUpdateApplicationConfigurationSettings(ApplicationType.FeedReader, config);

					CacheDependency dep = new CacheDependency(null, new string[]{CSConfiguration.CacheKey});
					string cacheKey = "ReaderConfiguration";
					CSCache.Insert(cacheKey, config, dep, CSCache.MinuteFactor*15);				
				}
			}
		}
	}
}
